package com.Questions.hash;

import java.util.*;
/**
 * 随机链表的复制
 */
public class leetcode138 {
    public Node copyRandomList(Node head) {
        if(head==null){
            return null;
        }
        Map<Integer,Node> map=new HashMap<>();
        Node List1=head;
        map.put(0,List1.random);
        Node List2=new Node(List1.val);
        Node head2=List2;
        int i=1;
        while (List1.next!=null){
            List1=List1.next;
            map.put(i,List1.random);
            i++;
            List2.next=new Node(List1.val);
            List2=List2.next;
        }
        i=0;
        List2=head2;
        while (List2.next!=null){
            List2.random=map.get(i);
            i++;
            List2=List2.next;

        }
        return head2;

    }
}
